package update.links.com.versionupdate;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.prim.update.impl.UpdateExplorer;
import com.prim.update.interfaces.IUpdateExplorer;
import com.prim.update.model.UpdateInfo;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：3/8 0008
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class UpdateDialog extends Dialog implements View.OnClickListener {
    private IUpdateExplorer explorer;

    private TextView tv_content, tv_message;

    private Button btn_close, btn_update;

    private UpdateExplorer.PrompterType type;

    public UpdateDialog(@NonNull Context context, IUpdateExplorer explorer, UpdateExplorer.PrompterType type) {
        super(context, R.style.Removedialog);
        this.explorer = explorer;
        this.type = type;
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.moudle_update_dialog, null);
        setContentView(view);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_message = (TextView) view.findViewById(R.id.tv_message);
        tv_message.setMovementMethod(new ScrollingMovementMethod());
        btn_close = (Button) view.findViewById(R.id.btn_close);
        btn_update = (Button) view.findViewById(R.id.btn_update);
        btn_close.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        UpdateInfo info = explorer.getInfo();
        String content = String.format("最新版本：%1$s\n\n更新内容\n%2$s", info.getVersion_name(), info.getMessage());
        tv_message.setText(content);
        if (type == UpdateExplorer.PrompterType.install) {
            btn_update.setText("立即安装");
            if (explorer.getBuilder().isIgnorable) {
                btn_close.setText("忽略该版");
            } else {
                btn_close.setText("取消");
            }
        } else if (explorer.getBuilder().isEnforce) {
            btn_update.setText("立即更新");
            btn_close.setVisibility(View.GONE);
        } else if (info.isEnforce()) {//服务器设置强制更新
            btn_update.setText("立即更新");
            btn_close.setVisibility(View.GONE);
        } else {
            if (type == UpdateExplorer.PrompterType.update) {
                btn_update.setText("立即更新");
                if (explorer.getBuilder().isIgnorable) {
                    btn_close.setText("忽略该版");
                } else {
                    btn_close.setText("取消");
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_update:
                explorer.setCancleUpdate(false);
                explorer.update();
                dismiss();
                break;
            case R.id.btn_close:
                if (explorer.getBuilder().isIgnorable) {
                    explorer.ignore();
                } else {
                    if (type == UpdateExplorer.PrompterType.install) {
                        explorer.stop();
                    } else {
                        explorer.setCancleUpdate(true);
                        explorer.update();
                    }
                }
                dismiss();
                break;
        }
    }
}
